<?php

namespace Mobile\Controller;

use Home\Logic\ZmdLogic;
use Think\Page;
use Think\Verify;

header('Content-Type:text/html;Charset=UTF-8');

class ZmdController extends BaseController {

    public $user_id = 0;
    public $user = array();

    /**
     * 初始化函数
     */
    public function _initialize() {
        parent::_initialize();
        if (session('?user')) {
            $user = session('user');
            $user = M('users')->where("user_id = {$user['user_id']}")->find();
            session('user', $user);  //覆盖session 中的 user
            $this->user = $user;
            $this->user_id = $user['user_id'];
            $this->assign('user', $user); //存储用户信息
        }
        //用户中心面包屑导航
        $navigate_user = navigate_user();
        $this->assign('navigate_user', $navigate_user);
    }

    /*
     * 会员自己激活自己
     */

    public function kthuiyuan() {
        $user = $this->user;
        $user_id = I('user_id', '');
        if ($user_id == '') {
            die('<script>alert("操作失败，请刷新页面后重试");history.go(-1);</script>');
        } else {
            $level = M('user_level')->where(array('level_id' => $user['level']))->find(); //会员等级查询
            $user_money = $level['amount'] * tpCache('reg.kt_2_1') / 100;
            $zuce_money = $level['amount'] * tpCache('reg.kt_2_2') / 100;
            if ($user['user_money'] < $user_money || $user['zuce_money'] < $zuce_money) {
                die('<script>alert("余额不足,请充值后重试");history.go(-1);</script>');
            }
            $res = M('users')->where('user_id=' . I('user_id'))->save(array('is_activate' => 1, 'jh_time' => time()));

            if ($res) {
                activate_log($user['user_id'], 3, '自助激活'); // 会员激活日志
                M('users')->where(array('user_id' => $user['user_id']))->setInc('total_amount', $level['amount']); // 累积消费
                bochu_day_log($level['amount'], 0);   // 拨出统计
                money($user['user_id'], $level['amount'], 0, 1, '注册激活投资'); // 增加投资记录
                accountLog($user_id, '-' . $user_money, '-' . $zuce_money, 0, 0, 0, '+' . $level['amount'], '注册激活投资');

                clear_tjj($user['first_leader'], $level['amount'], $user['nickname'] . '激活'); // 计算推荐奖   传推荐人id
                clear_bdj($user['zmd_id'], $level['amount'], $user['nickname'] . '激活'); // 计算报单奖   传专卖店id
                //clear_jdj($user['first_leader'], $level['amount'],$user['nickname'] . '激活');// 计算见点奖   传推荐人id
                $this->success("激活成功");
            } else {
                die('<script>alert("操作失败，请刷新页面后重试");history.go(-1);</script>');
            }
        }
    }

    public function test(){
        clear_bdj(16, 1000,'激活');// 计算见点奖   传推荐人id
    }
    
   

    /*
     * 推荐人激活会员 wxd 重写
     */

    public function tjr_kthuiyuan() {
        $user_id = I('user_id', '');
        //$user_id= 56;
        if ($user_id == '') {
            die('<script>alert("操作失败，请刷新页面后重试");history.go(-1);</script>');
        } else {
            $user = M('users')->where(array('user_id' => $user_id))->field('user_id,nickname,level,user_money,zuce_money,first_leader,zmd_id')->find();
            $first_leader = M('users')->where(array('user_id' => $user['first_leader']))->field('user_id,nickname,level,user_money,zuce_money,first_leader,zmd_id')->find();
            $level = M('user_level')->where(array('level_id' => $first_leader['level']))->find(); //服务人会员等级查询
            $user_money = $level['amount'];
           
            if ($first_leader['user_money'] < $user_money) {
                die('<script>alert("服务人库存币不足,请充值后重试");history.go(-1);</script>');
            }else{
                accountLog($user['first_leader'],'-'.$level['amount'],0,'+'.$level['amount'],0,0,0,0,'');
                //M('users')->where(array('user_id' =>$user['first_leader']))->setInc('total_amount', $level['amount']); // 累积消费
            }

            $data['is_activate']=1;
            $data['jh_time']=time();
            if ($level=1) {
                $data['level']=2;
            }
           $res = M('users')->where('user_id=' .  $user_id)->save($data);
            if ($res) {
                activate_log($user_id, 2, '管理员' . $_SESSION['admin_name'] . '激活'); // 会员激活日志
                share_j($user_id,$user['nickname'].'激活');
                bdfw_j($user_id,$user['nickname'] . '本店管理服务费');
                db_j($user_id,$user['nickname'] . '店补');
                $this->success("操作成功");
            } else {
                $this->error("操作失败");
            }
        }

    }



    /*
     * 报单中心激活会员
     */

    public function zmd_kthuiyuan() {
        $user = $this->user;
        $user_id = I('user_id', '');
        if ($user_id == '') {
            die('<script>alert("操作失败，请刷新页面后重试");history.go(-1);</script>');
        } else {
            $jhuser = M('users')->where('user_id=' . I('user_id'))->field('user_id,level,nickname,zmd_id,first_leader')->find(); // 查出要激活会员的资料
            $level = M('user_level')->where(array('level_id' => $jhuser['level']))->find(); //会员等级查询

            $user_money = $level['amount'] * tpCache('reg.kt_2_1') / 100;
            $zuce_money = $level['amount'] * tpCache('reg.kt_2_2') / 100;

            if ($user['user_money'] < $user_money || $user['zuce_money'] < $zuce_money) {
                die('<script>alert("余额不足,请充值后重试");history.go(-1);</script>');
            }
            $res = M('users')->where('user_id=' . I('user_id'))->save(array('is_activate' => 1, 'jh_time' => time()));
            if ($res) {
                activate_log($jhuser['user_id'], 5, '报单中心' . $user['nickname'] . '激活'); // 会员激活日志
                M('users')->where(array('user_id' => $user['user_id']))->setInc('total_amount', $level['amount']); // 累积消费
                bochu_day_log($level['amount'], 0);   // 拨出统计
                money($user_id, $level['amount'], 0, 1, '报单中心' . $user['nickname'] . '激活投资'); // 增加投资记录
                accountLog($user_id, '-' . $user_money, '-' . $zuce_money, 0, 0, 0, '+' . $level['amount'], '激活会员' . $jhuser['nickname']); // 扣掉激活会员所用的金额

                clear_tjj($jhuser['first_leader'], $level['amount'], $jhuser['nickname'] . '报单中心激活'); // 计算推荐奖   传推荐人id
                clear_bdj($jhuser['zmd_id'], $level['amount'], $jhuser['nickname'] . '报单中心激活'); // 计算报单奖   传专卖店id
                clear_jdj($jhuser['first_leader'], $level['amount'],$jhuser['nickname'] . '激活');// 计算见点奖   传推荐人id
                $this->success("激活成功");
            } else {
                die('<script>alert("操作失败，请刷新页面后重试");history.go(-1);</script>');
            }
        }
    }

    /*
     * 报单中心会员管理
     */

    public function zmd_list() {
        $user = $this->user;
        $condition = array();
        I('is_activate') ? $condition['is_activate'] = I('is_activate') : false;
        $condition['zmd_id'] = $user['user_id'];
        $model = M('users');
        $count = $model->where($condition)->count();
        $Page = new Page($count, 10);
        $zmd_list = $model->where($condition)->field('user_id,level,nickname,account,reg_time,mobile,is_lock,is_activate,jh_time,email')->order('is_activate')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach ($zmd_list as &$v) {
            $grade = M('user_level')->where(array('level_id' => $v['level']))->field('level_name')->find();
            $v['level_name'] = $grade['level_name'];
        }
        $show = $Page->show();
        $this->assign('zmd_list', $zmd_list);
        $this->assign('page', $show); // 赋值分页输出
        $this->display();
    }

    /*
     * 直推会员管理
     */

    public function tjr_list() {
        $user = $this->user;
        $condition = array();
        /*if (I('is_activate') != 'all') {
            I('is_activate') ? $condition['is_activate'] = I('is_activate') : $condition['is_activate'] = 0;
        }*/
        $condition['first_leader'] = $user['user_id'];

        $model = M('users');
        $count = $model->where($condition)->count();
        $Page = new Page($count,10);
        $tjr_list = $model->where($condition)->field('user_id,level,nickname,account,reg_time,mobile,is_lock,is_activate,jh_time,email')->order('is_activate,reg_time desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach ($tjr_list as &$v) {
            $grade = M('user_level')->where(array('level_id' => $v['level']))->field('level_name')->find();
            $v['level_name'] = $grade['level_name'];
        }
        $show = $Page->show();
        $this->assign('tjr_list', $tjr_list);


        if($_GET['is_ajax'])
        {   
            $this->display('ajax_tjr_list');
            exit;
        }  
        $this->assign('page', $show); // 赋值分页输出
        $this->display();
    }

    /*
     * 报单中心申请
     */

    public function zmd_add() {
        $user = $this->user;
        if (IS_POST) {
            $note = I('post.note', '');
            if ($note == '') {
                die('<script>alert("申请理由不能为空，请刷新页面后重试");history.go(-1);</script>');
            }
            $_POST['u_id'] = $user['user_id'];
            $_POST['time'] = time();
            $_POST['ztrs'] =I('post.ztrs');
            $_POST['tdyj'] =I('post.tdyj');
            $_POST['type'] =1;
            $_POST['note'] =I('post.note');
            $reg = M('user_zmd')->add($_POST);
            if ($reg) {
                M('users')->where('user_id=' . $user['user_id'])->save(array('zmd' => 2));
                $this->success("提交成功");
            } else {
                die('<script>alert("操作失败，请刷新页面后重试");history.go(-1);</script>');
            }
        } else {

            $this->display();
        }
    }

//会员树状分销图
    public function team() {
        $user = $this->user;
        $this->aa = $user['user_id'];  // 会员列表传值过来 如果没有，就黙认 为第一条
        $this->display();
    }

//查出团队信息操作
    public function getTreeBaseInfo($id) {
        if (!$id)
            return;
        $r = M("users")->where(array('user_id' => $id))->field('user_id,first_leader,mobile,nickname,email,account')->find(); // 查出要查找的会员字段
//判断传过来的值能不能查出数据，不能就用系统的
        if ($r['user_id'] != '') {
            $total = M('users')->where("first_leader='{$r['user_id']}'")->count();
        } else {
            $total = M('users')->where("first_leader='1'")->count();
        }
        if ($r)
            $_SESSION['tuan_dui'] = 0;
        $this->getTreeCount($r['user_id']);
        return array(
            "id" => $r['user_id'],
            "pId" => $r['first_leader'],
            // "name" => '　手机号：' . $r['mobile'] . '，邮箱' . $r['email'] . '，昵称' . $r['nickname'] . '，直推人数：' . $total . '，团队人数总：' . $_SESSION['tuan_dui']);
            "name" =>  '账号' . $r['account'] . '，直推人数：' . $total . '，团队人数总：' . $_SESSION['tuan_dui']);
    }

//统计用户的团队人数
    public function getTreeCount($id) {
        $ids = self::get_childs($id);
        if (!$ids) {
            return $trees;
        }
        foreach ($ids as $v) {
            $_SESSION['tuan_dui'] ++;
            $this->getTreeCount($v);
        }
    }

    public function getTreeInfo($id) {
        static $trees = array();
        $ids = self::get_childs($id);
        if (!$ids) {
            return $trees;
        }

        foreach ($ids as $v) {
            $trees [] = $this->getTreeBaseInfo($v);
            //$this->getTreeInfo($v); 只显示一代
        }
        return $trees;
    }

//统计用户团队的人数
    public static function get_childs($id) {
        if (!$id)
            return null;
        $childs_id = array();
        $childs = M("users")->where(array('first_leader' => $id))->field('user_id')->select();
        foreach ($childs as $v) {
            $childs_id [] = $v ['user_id'];
        }
        if ($childs_id)
            return $childs_id;
        return 0;
    }

//查出团队信息的操作
    public function getTree() {
        if (I('post.user1') <> '0') {
            $getuser = I('post.user1');
        } else {
            $getuser = '1';
        }
        $base = $this->getTreeBaseInfo($getuser);
        $znote = $this->getTreeInfo($getuser);
        $znote [] = $base;
        echo json_encode(array("status" => 0, "data" => $znote));
    }

//指定查询用户的团队信息
    public function getTreeso() {
        if ($_POST['user'] <> '') {
            $user = M('users')->where("mobile='{$_POST['user']}' OR email='{$_POST['user']}'")->field('user_id')->find();
            if (!$user) {
                echo json_encode(array("status" => 1, "data" => '用戶不存在!'));
            } else {
                $base = $this->getTreeBaseInfo($user['user_id']);
                $znote = $this->getTreeInfo($user['user_id']);
                $znote [] = $base;
                echo json_encode(array("status" => 0, "data" => $znote));
            }
        } else {
            $base = $this->getTreeBaseInfo(1);
            $znote = $this->getTreeInfo(1);
            $znote [] = $base;
            echo json_encode(array("status" => 0, "data" => $znote));
        }
    }

    /**
     * 网络结构图
     */
    public function arrangefor() {
        $user = M('users');
        $dquser = $this->user;
        $grade = M('user_level');
        if (!empty($_POST['user'])) {
            $account = $_POST['user'];
            $user_id = M('users')->where("account='{$account}'")->field('user_id,account')->find();
            if(!$user_id){
                $this->error('该会员不存在');
            }
            $map['user_id'] = $user_id['user_id'];
        } else {
            $user_id['account'] = $dquser['account'];
            $map['user_id'] = $dquser['user_id'];
        }
        $res = $user->field('user_id,account,level,jh_time,is_activate')->where($map)->find();
        $branch = M('user_branch')->where(array('user_id'=>$res['user_id']))->find();
        $res = array_merge($res,$branch);
        $v = $grade->field('level_id,level_name')->where('level_id=' . $res['level'])->find();
        $res['grade_id'] = $v['level_name'];
        $res['activate'] = $res['is_activate'] == 1 ? '未激活' : '已激活';
        $this->list = $res;
        $old['l_man'] = count_man($dquser['user_id'], 1);
        $old['r_man'] = count_man($dquser['user_id'], 2);
//        dump($res);die;
//        dump($res);
//        unset($_SESSION['tuan_dui']);
//        jlsja($_SESSION['user']['account']);
//        $old['td_man'] = $_SESSION['tuan_dui'];
        $old['td_man'] = $old['l_man'] + $old['r_man'];
        $old['zt_man'] = $user->where(array('first_leader' =>$dquser['user_id']))->count();
        $this->old = $old;
        $this->user2_id = $user_id['account'];
        $this->display();
    }

    // 安置网络列表
    public function network() {
        $account = $_GET['user'];
        $user_id = M('users')->where("account='{$account}'")->getField('user_id');
        $this->abcd($user_id, 1);
    }


    public function yyy() {
        $use = M('users')->where("account='" . $_GET['use'] . "'")->find();
        $user_branch = M('user_branch')->where(array('user_id'=>$use['user_id']))->field('jdr_id')->find();
        $account = $user_branch['jdr_id'];
        $this->abcd($account, 1);
    }
    public function abcd($ab, $k) {
        $account = $ab;
        $old = M('user_branch')->where("user_id='" . $account . "'")->find();
        $firstuser = M('users')->where(array('user_id'=>$old['user_id']))->field('level,is_activate,nickname,account,jh_time')->find();
        $old = array_merge($old,$firstuser);
        $path = $old['path'] . ',' . $old['user_id'];

        $user = M('user_branch')->where("path='" . $path . "'")->order('path')->select();
        foreach($user as &$v){
            $user_v1 = M('users')->field('account,level,nickname,jh_time,is_activate')->where(array('user_id'=>$v['user_id']))->find();
            $v['level'] = $user_v1['level'];
            $v['account'] = $user_v1['account'];
            $v['username'] = $user_v1['nickname'];
            $v['is_activate'] = $user_v1['is_activate'];
            $v['jhtime'] = date('Y-m-d H:i:s',$user_v1['jh_time']);
        }
        static $res = array();

        $g = M('user_level')->field('level_id,level_name')->find($old['level']);
        $old['grade_id'] = $g['level_name'];
        $old['jhtime'] = date('Y-m-d H:i:s',$old['jh_time']);
        $old['activate'] = $old['is_activate'] == 1 ? '未激活' : '已激活';
        $i = pow(2, $k - 1);
        $res[10 * $i] = $old;
//    dump($user);
        if (count($user) > 1) {
            foreach ($user as $val) {
                $s = $val['position'] -1;
//                dump($s);
                $a = M('user_level')->field('level_id,level_name')->find($val['level']);
                $val['grade_id'] = $a['level_name'];
                $val['activate'] = $val['is_activate'] == 1 ? '未激活' : '已激活';
                $res[20 * $i + $s] = $val;

                $path1 = $val['path'] . ',' . $val['user_id'];
                $user1 = M('user_branch')->where("path='" . $path1 . "'")->order('path')->select();
                foreach($user1 as &$v1){
                    $user_v2 = M('users')->field('account,level,nickname,jh_time,is_activate')->where(array('user_id'=>$v1['user_id']))->find();
                    $v1['level'] = $user_v2['level'];
                    $v1['account'] = $user_v2['account'];
                    $v1['username'] = $user_v2['nickname'];
                    $v1['is_activate'] = $user_v2['is_activate'];
                    $v1['jhtime'] = date('Y-m-d H:i:s',$user_v2['jh_time']);
                }
                if (count($user1) > 1) {
                    foreach ($user1 as $val1) {
                        $s1 = $val1['position'] - 1;
                        $a1 = M('user_level')->field('level_id,level_name')->find($val1['level']);
                        $val1['grade_id'] = $a1['level_name'];
                        $val1['activate'] = $val1['is_activate'] == 1 ? '未激活' : '已激活';
                        $res[(20 * $i + $s) * 2 + $s1] = $val1;

                        $path2 = $val1['path'] . ',' . $val1['user_id'];
//                        $user2 = M('users')->field('id,path,account,grade_id,jhtime,activate,left_total,left_new,username,jdr_number,prev_knot,position,right_new,right_total')->order('path')->where("path='" . $path2 . "'")->select();
                        $user2 = M('user_branch')->where("path='" . $path2 . "'")->order('path')->select();
                        foreach($user2 as &$v2){
                            $user_v3 = M('users')->field('account,level,nickname,jh_time,is_activate')->where(array('user_id'=>$v2['user_id']))->find();
                            $v2['level'] = $user_v3['level'];
                            $v2['account'] = $user_v3['account'];
                            $v2['username'] = $user_v3['nickname'];
                            $v2['is_activate'] = $user_v3['is_activate'];
                            $v2['jhtime'] = date('Y-m-d H:i:s',$user_v3['jh_time']);
                        }
                        if (count($user2) > 1) {
                            foreach ($user2 as $val2) {
                                $s2 = $val2['position'] - 1;
                                $a2 = M('user_level')->field('level_id,level_name')->find($val1['level']);
                                $val2['grade_id'] = $a2['level_name'];
                                $val2['activate'] = $val2['is_activate'] == 1 ? '未激活' : '已激活';
                                $res[((20 * $i + $s) * 2 + $s1) * 2 + $s2] = $val2;
//                                $this->ajaxReturn($res);
                            }
                        } elseif (count($user2) == 1) {
                            $a2 = M('user_level')->field('level_id,level_name')->find($user2[0]['level']);
                            $user2[0]['grade_id'] = $a2['level_name'];
                            $user2[0]['activate'] = $user2[0]['is_activate'] == 1 ? '未激活' : '已激活';
                            $res[(20 * $i + $s) * 4] = $user2[0];
                        }
                    }
                } elseif (count($user1) == 1) {
                    $a1 = M('user_level')->field('level_id,level_name')->find($user1[0]['level']);
                    $user1[0]['grade_id'] = $a1['level_name'];
                    $user1[0]['activate'] = $user1[0]['is_activate'] == 1 ? '未激活' : '已激活';
                    $res[(20 * $i + $s) * 2] = $user1[0];

                    $path2 = $user1[0]['path'] . ',' . $user1[0]['user_id'];
//                    $user2 = M('users')->field('id,path,account,grade_id,jhtime,activate,left_total,left_new,username,jdr_number,prev_knot,position,right_new,right_total')->order('path')->where("path='" . $path2 . "'")->select();
                    $user2 = M('user_branch')->where("path='" . $path2 . "'")->order('path')->select();
                    foreach($user2 as &$v2){
                        $user_v3 = M('users')->field('account,level,nickname,jh_time,is_activate')->where(array('user_id'=>$v2['user_id']))->find();
                        $v2['level'] = $user_v3['level'];
                        $v2['account'] = $user_v3['account'];
                        $v2['username'] = $user_v3['nickname'];
                        $v2['is_activate'] = $user_v3['is_activate'];
                        $v2['jhtime'] = date('Y-m-d H:i:s',$user_v3['jh_time']);
                    }

                    if (count($user2) > 1) {
                        foreach ($user2 as $val2) {
                            $s2 = $val2['position'] - 1;
                            $a2 = M('user_level')->field('level_id,level_name')->find($val2['level']);
                            $val2['grade_id'] = $a2['level_name'];
                            $val2['activate'] = $val2['is_activate'] == 1 ? '未激活' : '已激活';
                            $res[(20 * $i + $s) * 4 + $s2] = $val2;
                        }
                    } elseif (count($user2) == 1) {
                        $a2 = M('user_level')->field('level_id,level_name')->find($user2[0]['level']);
                        $user2[0]['grade_id'] = $a2['level_name'];
                        $user2[0]['activate'] = $user2[0]['is_activate'] == 1 ? '未激活' : '已激活';
                        $res[(20 * $i + $s) * 4] = $user2[0];
                    }
                }
            }
        } elseif (count($user) == 1) {
            $a = M('user_level')->field('level_id,level_name')->find($user[0]['level']);
            $user[0]['grade_id'] = $a['level_name'];
            $user[0]['activate'] = $user[0]['is_activate'] == 1 ? '未激活' : '已激活';
            $res[20 * $i] = $user[0];

            $path1 = $user[0]['path'] . ',' . $user[0]['user_id'];
//            $user1 = M('users')->field('id,path,account,grade_id,jhtime,activate,left_total,left_new,username,jdr_number,prev_knot,position,right_new,right_total')->order('path')->where("path='" . $path1 . "'")->select();
            $user1 = M('user_branch')->where("path='" . $path1 . "'")->order('path')->select();
            foreach($user1 as &$v1){
                $user_v3 = M('users')->field('account,level,nickname,jh_time,is_activate')->where(array('user_id'=>$v1['user_id']))->find();
                $v1['level'] = $user_v3['level'];
                $v1['account'] = $user_v3['account'];
                $v1['username'] = $user_v3['nickname'];
                $v1['is_activate'] = $user_v3['is_activate'];
                $v1['jhtime'] = date('Y-m-d H:i:s',$user_v3['jh_time']);
            }
            if (count($user1) > 1) {
                foreach ($user1 as $val1) {
                    $s1 = $val1['position'] - 1;
                    $a1 = M('user_level')->field('level_id,level_name')->find($val1['level']);
                    $val1['grade_id'] = $a1['level_name'];
                    $val1['activate'] = $val1['is_activate'] == 1 ? '未激活' : '已激活';
                    $res[40 * $i + $s1] = $val1;

                    $path2 = $val1['path'] . ',' . $val1['user_id'];
//                    $user2 = M('users')->field('id,path,account,grade_id,jhtime,activate,left_total,left_new,username,jdr_number,prev_knot,position,right_new,right_total')->order('path')->where("path='" . $path2 . "'")->select();
                    $user2 = M('user_branch')->where("path='" . $path2 . "'")->order('path')->select();
                    foreach($user2 as &$v2){
                        $user_v3 = M('users')->field('account,level,nickname,jh_time,is_activate')->where(array('user_id'=>$v2['user_id']))->find();
                        $v2['level'] = $user_v3['level'];
                        $v2['account'] = $user_v3['account'];
                        $v2['username'] = $user_v3['nickname'];
                        $v2['is_activate'] = $user_v3['is_activate'];
                        $v2['jhtime'] = date('Y-m-d H:i:s',$user_v3['jh_time']);
                    }
                    if (count($user2) > 1) {
                        foreach ($user2 as $val2) {
                            $s2 = $val2['position'] - 1;
                            $a2 = M('user_level')->field('level_id,level_name')->find($val1['level']);
                            $val2['grade_id'] = $a2['level_name'];
                            $val2['activate'] = $val2['is_activate'] == 1 ? '未激活' : '已激活';
                            $res[(40 * $i + $s1) * 2 + $s2] = $val2;
                        }
                    } elseif (count($user2) == 1) {
                        $v2 = M('user_level')->field('level_id,level_name')->find($user2[0]['level']);
                        $user2[0]['grade_id'] = $v2['level_name'];
                        $user2[0]['activate'] = $user2[0]['is_activate'] == 1 ? '未激活' : '已激活';
                        $res[(40 * $i + $s1) * 2] = $user2[0];
                    }
                }
            } elseif (count($user1) == 1) {
                $a1 = M('user_level')->field('level_id,level_name')->find($user1[0]['level']);
                $user1[0]['grade_id'] = $a1['level_name'];
                $user1[0]['activate'] = $user1[0]['is_activate'] == 1 ? '未激活' : '已激活';
                $res[40 * $i] = $user1[0];

                $path2 = $user1[0]['path'] . ',' . $user1[0]['user_id'];
//                $user2 = M('users')->field('id,path,account,grade_id,jhtime,activate,left_total,left_new,username,jdr_number,prev_knot,position,right_new,right_total')->order('path')->where("path='" . $path2 . "'")->select();
                $user2 = M('user_branch')->where("path='" . $path2 . "'")->order('path')->select();
                foreach($user2 as &$v2){
                    $user_v3 = M('users')->field('account,level,nickname,jh_time,is_activate')->where(array('user_id'=>$v2['user_id']))->find();
                    $v2['level'] = $user_v3['level'];
                    $v2['account'] = $user_v3['account'];
                    $v2['username'] = $user_v3['nickname'];
                    $v2['is_activate'] = $user_v3['is_activate'];
                    $v2['jhtime'] = date('Y-m-d H:i:s',$user_v3['jh_time']);
                }

                if (count($user2) > 1) {
                    foreach ($user2 as $val2) {
                        $s2 = $val2['position'] - 1;
                        $a2 = M('user_level')->field('level_id,level_name')->find($val2['level']);
                        $val2['grade_id'] = $a2['level_name'];
                        $val2['activate'] = $val2['is_activate'] == 1 ? '未激活' : '已激活';
                        $res[80 * $i + $s2] = $val2;
                    }
                } elseif (count($user2) == 1) {
                    $a2 = M('user_level')->field('level_id,level_name')->find($user2[0]['level']);
                    $user2[0]['grade_id'] = $a2['level_name'];
                    $user2[0]['activate'] = $user2[0]['is_activate'] == 1 ? '未激活' : '已激活';
                    $res[80 * $i] = $user2[0];
                }
            }
        }
        $this->ajaxReturn($res);
    }

}
